﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    public class FileTitleBar:Panel
    {
        public List<TitlePanel> titlePanelList = new List<TitlePanel>();

        private int filenum = 0;
        private TitlePanel NowTitle = null;  //当前被选中的标题面板
        private MainPanel mainPanel= null;
        public SubPanel subPanel= null;

        public int Left_dat = 0;

        private Label bottomLabel;
        private CtrlBar ctrlBar;

        public FileTitleBar(MainPanel _mainPanel, SubPanel _subPanel)
        {
            mainPanel = _mainPanel;
            subPanel = _subPanel;

            Location = new Point(0, 0);
            Size = new Size(subPanel.Width, 60);
            BackColor = Color.FromArgb(255, 1, 4, 9);

            bottomLabel = new Label()
            {
                Name = "labelline",
                Location = new Point(0, this.Height - 1),
                Size = new Size(4096, 1),
                BackColor = Color.FromArgb(48, 54, 61),
                Cursor = Cursors.Default,
            };

            ctrlBar = new CtrlBar(this);

            this.Controls.Add(bottomLabel);
            this.Controls.Add(ctrlBar);


            Label label = new Label()
            {
                Size = new Size(38, 38),
                Location = new Point(8, (this.Size.Height - 38) / 2),
                BackColor = this.BackColor,
                Image = AllImage.FImage_file[10],
                Cursor = Cursors.Hand,
            };

            label.MouseDown += new MouseEventHandler(Split_MouseDown);
            label.MouseUp += new MouseEventHandler(Split_MouseUP);
            label.MouseEnter += new EventHandler(Split_MouseEnter);
            label.MouseLeave += new EventHandler(Split_MouseLeave);

            ctrlBar.AddBtu(label);
        }

        //public void AddTitlePanel(Inode inode)
        //{
        //    /*判断这文件是否已经在标题栏*/
        //    foreach (TitlePanel tmpTitle in titlePanelList)
        //    {
        //        if (inode.Equals(tmpTitle.TitleInode))
        //        { //已经存在  改为选中
        //            SetSelected(inode);
        //            return;
        //        }
        //    }

        //    int cnt = titlePanelList.Count;
        //    TitlePanel tmp = new TitlePanel(inode, this.Height, this);

        //    this.Controls.Add(tmp);
        //    titlePanelList.Add(tmp);
        //    filenum++;
        //    NowTitle = tmp;
        //    tmp.SetCnt(cnt, Left_dat);
        //    SetSelected(inode);
        //    RefreshLabel();
        //}

        //public void RmTitlePanel(Inode inode)
        //{
        //    bool flag = false;
        //    if (inode == null) return;

        //    int _cnt = -1, now_cnt = -1;

        //    foreach (TitlePanel tmp in titlePanelList)
        //    {
        //        if (inode == tmp.TitleInode)
        //        {
        //            inode.DeleteMonitor(subPanel);//删除这个

        //            this.Controls.Remove(tmp);
        //            titlePanelList.Remove(tmp);
        //            flag = true;

        //            filenum--;
        //            _cnt = tmp.cnt;
        //            break;
        //        }
        //    }
        //    if (!flag) return;


        //    if (NowTitle != null)
        //    {
        //        now_cnt = NowTitle.cnt;
        //        if (_cnt == now_cnt) //当前选中的文件栏被关闭
        //        {
                    
        //            if (titlePanelList.Count <= _cnt)//当前关闭的时最后一个
        //            {
        //                if (titlePanelList.Count > 0)
        //                {
        //                    SetSelected(titlePanelList[titlePanelList.Count - 1]);
        //                }
        //                else
        //                { //所有的栏都关了
        //                    filenum = 0;
        //                    NowTitle = null;
        //                    mainPanel.RmSubPanel(subPanel);
        //                }
        //            }
        //            else
        //            {
        //                SetSelected(titlePanelList[_cnt]);//把后一个显示出来
        //            }
        //        }
        //        RefreshCnt();
        //    }
        //    RefreshLabel();
        //}

        public void RefreshCnt()
        {
            int i = 0;
            foreach (TitlePanel tmp in titlePanelList)
            {
                tmp.SetCnt(i, Left_dat);
                i++;
            }
        }

        public void RefreshLabel()
        {
            int _Left = 0;
            if (titlePanelList.Count != 0)
            {
                TitlePanel _tmp = titlePanelList[titlePanelList.Count - 1];
                _Left = _tmp.Left + _tmp.Width;
            }

            ctrlBar.RefreshLocation();  //刷新控制小窗口

            bottomLabel.Left = _Left;
            bottomLabel.Width = this.Width;//这里这样做是错的//但是效果一样  
        }

        public void SetSelected(TitlePanel tmp)  //设置当前选中的TitlePanel
        {
            //SetSelected(tmp.TitleInode);
        }

        //public void SetSelected(Inode tmp)  //设置当前选中的TitlePanel
        //{
        //    foreach (TitlePanel _tmp in titlePanelList)
        //    {
        //        if (tmp.Equals(_tmp.TitleInode))
        //        {
        //            NowTitle = _tmp;
        //            _tmp.SetSelec(true);
        //            _tmp.inode.ObserverFile(subPanel);
        //        }
        //        else
        //        {
        //            _tmp.SetSelec(false);
        //            _tmp.inode.DeleteSubPanel(subPanel);
        //        }
        //    }
        //}

        private bool focus1 = false;  //表示当前是否拥有焦点  

        public bool Focus1 
        {
            get => focus1;
            set { 
                focus1 = value;
                foreach (TitlePanel _tmp in titlePanelList)
                {
                    _tmp.Focus1 = value;
                }
                ctrlBar.Focus1 = value;
            }
        }


        private bool Mouseflag = false;
        private void Split_MouseUP(object sender, EventArgs e)
        {
            if (Mouseflag)
            {
                if (mainPanel != null)
                {
                    //mainPanel.AddSubPanel(NowTitle.TitleInode);
                }
            }
            Mouseflag = false;
        }
        private void Split_MouseDown(object sender, EventArgs e)
        {
            Mouseflag = true;
        }

        private void Split_MouseEnter(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            label.Image = AllImage.FImage_file[11];
        }
        private void Split_MouseLeave(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            label.Image = AllImage.FImage_file[10];
        }
    }

}